<?php
	require_once 'environment.php';
	//Admins only!
	if(!$acc->isLogged()) die(change_site("account.php"));
	//The submenus...
	$temp->currentPage("Home >> Account >> Create Account");
	$temp->addSubMenu("Create Account", "createAccount.php");
	$temp->addSubMenu("Change Password", "editAccount.php");
	$temp->addSubMenu("Delete Account", "deleteAccount.php");
	$temp->addSubMenu("Logout", "account.php?action=Logout");
	//This selects the account tab and then the create account tab...
	$temp->setTab(9, 0);
	$temp->generate();
	
	function throwError($err) {
		$err = urlencode($err);
		print change_site("createAccount.php?error={$err}");
	}
	
	if(!fixArr($_POST, "nuName")) {
		print "<form name=\"newAdmin\" action=\"\" method=\"POST\">\n";
		if(fixArr($_GET, "error")) {
			print "<span style=\"color: red;\">".fixArr($_GET, "error")."</span><br />\n";
		}
		print "<label>Name: <input type=\"text\" name=\"nuName\" /></label><br />\n";
		print "<label>Password: <input type=\"password\" name=\"pass\" /></label><font size=\"-2\">Must be 6 characters long at least!</font><br />\n";
		print "<label>Confirm Password: <input type=\"password\" name=\"cpass\" /></label><br />\n";
		print "<label>Security Question: <input type=\"text\" name=\"seqQuest\" /></label><br />\n";
		print "<label>Security Answer: <input type=\"text\" name=\"seqAnsw\" /></label><br />\n";
		print "<input type=\"submit\" value=\"Create\" /> <input type=\"reset\" value=\"Clear Form\" />";
		print "</form>\n";
	}else {
		foreach($_POST as $k => $field) {
			$_POST[$k] = trim($field);
			if(empty($_POST[$k])) throwError("A field was left blank.");
		}
		if(fixArr($_POST, "pass") != fixArr($_POST, "cpass")) throwError("Passwords did not match.");
		$userName = fixArr($_POST, "nuName");
		$try = $acc->addAdmin($userName, fixArr($_POST, "pass"), fixArr($_POST, "seqQuest"), fixArr($_POST, "seqAnsw"));
		if($try) {
			print "The account has been successfully created!<br />\nThe administrator can log in and change their password now.";
		}else if($try === 0) {
			throwError("The password must be 6 characters long.");
		}else {
			throwError("The user already exists.");
		}
	}
?>